package com.stone;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
/**
 * Region.OP表示图层的混合方法，其是一个枚举值，其值有：
 * DIFFERENCE 去掉差异
 * INTERSECT 加入(默认的方式)
 * REPCLACE 替换
 * REVERSE_DEFFERENCE 保留差异
 * UNION 和
 * XOR 异或
 * clipPath和clipRect等方法用于在将画布内的几个区域裁减掉。
 * clipPath(Path path)
 * clipPath(Path path,Region.OP op)
 * clipRect(Rect rect)
 * clipRect(float left,float top,float right,float button)
 * clipRect(float left,float top,float right,float button,Region.OP op)
 * */
public class ClippingActivity extends Activity {
  @Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(new MyView(this));
}
   private static class MyView extends View{
    private Paint mPaint;
    private Path mPath;
	public MyView(Context context) {
		super(context);
		setFocusable(true);
		mPaint=new Paint();
		//是否消除锯齿
		mPaint.setAntiAlias(true);
		//设置画笔的笔画宽度
		mPaint.setStrokeWidth(6);
		//设置画笔文本大小，必须大于0
		mPaint.setTextSize(16);
		//设置画笔文本的对齐方式
		mPaint.setTextAlign(Paint.Align.RIGHT);
		
		mPath=new Path();
	}
	private void drawCanvas(Canvas canvas){
		//选择区域
		canvas.clipRect(0, 0, 100, 100);
		canvas.drawColor(Color.WHITE);
		//设置画笔的颜色
		mPaint.setColor(Color.RED);
		//画直线
		canvas.drawLine(0, 0,100, 100, mPaint);
		//用绿色的画笔画圆
		mPaint.setColor(Color.GREEN);
		canvas.drawCircle(30, 70, 30, mPaint);
		//用蓝色笔，画文本
		mPaint.setColor(Color.BLUE);
		canvas.drawText("android", 100, 30, mPaint);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		//canvas.save();
		//设置画布背景色
		canvas.drawColor(Color.GRAY);
		//重置坐标原点
		canvas.translate(10, 10);//左上部分
		drawCanvas(canvas);
	 	//重新设置canvas原点坐标
		canvas.restore();
		//保存设置，如果没有以上这两句代码，是看不到后来的图形
		canvas.save();
		canvas.translate(160, 10); //右上部分，这里可以看出左上部分的坐标
		
		canvas.clipRect(10, 10, 90, 90);
		
		canvas.clipRect(30, 30, 70, 70,Region.Op.DIFFERENCE);//中间的矩形被出去
		drawCanvas(canvas);
		
		canvas.restore();
		canvas.save();
		//左中间部分
		canvas.translate(10, 160);
		mPath.reset();
		canvas.clipPath(mPath);
		//做一个圆
		mPath.addCircle(50, 50, 50, Path.Direction.CCW);
		//圆的外部被除去,替代
		canvas.clipPath(mPath,Region.Op.REPLACE);
		drawCanvas(canvas);
		canvas.restore();
		canvas.save();
      	//右中间部分
		canvas.translate(160, 160);
		canvas.clipRect(0, 0, 60, 60);
		//获取两个裁减的交集（和）
		canvas.clipRect(40, 40, 100, 100,Region.Op.UNION);
		drawCanvas(canvas);
		canvas.restore();
		canvas.save();
		
		canvas.translate(10, 310);
		canvas.clipRect(0, 0, 60, 60);
		//异或 两个部分
		canvas.clipRect(40, 40, 100, 100,Region.Op.XOR);
		drawCanvas(canvas);
		canvas.restore();
		canvas.save();
		
		canvas.translate(160, 310);
		canvas.clipRect(0, 0, 60, 60);
		//保留差异，补集
		canvas.clipRect(40, 40, 100, 100,Region.Op.REVERSE_DIFFERENCE);
	    drawCanvas(canvas);
	    canvas.restore();
	}
   }
}
